package com.csun.nusing.ui.familyactivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.presenter.LoginPresenter;
import com.csun.nusing.presenter.inter.ILogin;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.ui.activity.ResignActivity;
import com.csun.nusing.util.IntentUtils;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.StringUtils;

/**
 * 登录界面 自动记住密码 ClassName: LoginActivity
 * 
 * @Description: 自动登录
 * @author fzq
 * @date 2017-10-18
 */
public class FamilyLoginActivity extends BaseActivity<LoginPresenter> implements
		ILogin {

	private EditText account_et;
	private EditText password_et;
	private Button confirm_bt;
	private String username;
	private String password;
	private TextView forgerpasswordtv;
	private TextView resigntv;

	@Override
	public int getContentViewId() {
		return R.layout.activity_family_login;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
	}

	private void findView() {
		account_et = findViewById(R.id.account_et);
		password_et = findViewById(R.id.password_et);
		forgerpasswordtv = findViewById(R.id.forget_password_tv);
		resigntv = findViewById(R.id.login_resign_tv);
		confirm_bt = findViewById(R.id.confirm_bt);
		confirm_bt.setOnClickListener(new click());
		resigntv.setOnClickListener(new click());
		forgerpasswordtv.setOnClickListener(new click());
	}

	/**
	 * 登录
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	protected void Login() {
		mPresenter.login(this, account_et, password_et);
		confirm_bt.setEnabled(false);
		//5s后可以点击
		handler.sendEmptyMessageDelayed(0x89, 5000);
	}

	Handler handler = new Handler() {
		@Override
		public void dispatchMessage(Message msg) {
			super.dispatchMessage(msg);
			if (msg.what == 0x89) {
				confirm_bt.setEnabled(true);
			}
		}
	};

	/**
	 * 注册
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	public void resign() {
		IntentUtils.startActivity(this, ResignActivity.class);
	}

	/**
	 * 忘记密码
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	public void forgetPsd() {
		IntentUtils.startActivity(this, ForgetPsdActivity.class);
	}

	@Override
	public void initData() {
		super.initData();
		// 从sp获取值
		username = (String) SPUtils.get(this, "USERNAME", "");
		password = (String) SPUtils.get(this, "PASSWORD", "");
		// cbCheckFlag = (boolean) SPUtils.get(this, "cbCheckFlag", false);
		if (!StringUtils.isEmpty(username)) {
			account_et.setText(username);
		}
		// 如果记住密码
		// if (cbCheckFlag) {
		// re_password.setChecked(cbCheckFlag);
		if (!StringUtils.isEmpty(password)) {
			password_et.setText(password);
		}
		// }
	}

	private class click implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.confirm_bt:
				Login();
				break;
			case R.id.login_resign_tv:
				resign();
				break;
			case R.id.forget_password_tv:
				forgetPsd();
				break;
			default:
				break;
			}
		}
	}

	@Override
	protected LoginPresenter getPresenter() {
		return new LoginPresenter(this);
	}

}
